package com.yuwei.parkinglot.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import androidx.databinding.DataBindingUtil.bind
import androidx.databinding.DataBindingUtil.setContentView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.yuwei.base.BaseActivity
import com.yuwei.base.BaseViewModel
import com.yuwei.base.path.RouterPath
import com.yuwei.http.entity.ParkingLotEntityItem
import com.yuwei.parkinglot.adapter.ParkingLotAdapter
import com.yuwei.parkinglot.databinding.ActivityParkingLotBinding
import com.yuwei.parkinglot.viewmodel.ParkingLotViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.MODULE_PARKING_LOT_ACTIVITY)
class ParkingLotActivity : BaseActivity<ActivityParkingLotBinding, ParkingLotViewModel>() {
    val parkingLotAdapter = ParkingLotAdapter()
    val parkingLotList = mutableListOf<ParkingLotEntityItem>()
    override fun initView() {
        vm.getParkingLotList(1)
        binding.parkingLotRv.adapter = parkingLotAdapter
        binding.parkingLotRv.layoutManager = LinearLayoutManager(this)
        binding.parkingLotBack.setOnClickListener { finish() }
        binding.parkingLotAdd.setOnClickListener {
            ARouter.getInstance().build(RouterPath.MODULE_ADD_PARKING_ACTIVITY).withInt("parkingLotId",-1).navigation()
        }
        parkingLotAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(RouterPath.MODULE_ADD_PARKING_ACTIVITY)
                .withInt("parkingLotId",parkingLotAdapter.data[position].id)
                .navigation()
        }
        binding.parkingLotSearch.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val filter = parkingLotList.filter {
                    it.spaceNumber.contains(s.toString())
                }
                parkingLotAdapter.data.clear()
                parkingLotAdapter.data.addAll(filter)
                parkingLotAdapter.notifyDataSetChanged()

            }

            override fun afterTextChanged(s: Editable?) {
                if(s.toString().isEmpty()){
                    vm.getParkingLotList(1)
                }
            }

        })
    }

    override fun initData() {
        vm.getParkingLotListSuccess.observe(this){
            Log.i("TAG", "initData: ${it[0].areaName}")
            parkingLotList.addAll(it)
            parkingLotList.reverse()
            parkingLotAdapter.data.addAll(parkingLotList)
            parkingLotAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getParkingLotList(1)
    }

}